﻿Option Explicit On

Imports System.Xml
Imports System.Xml.Schema
Imports System.IO
Imports System.Web.UI.WebControls

Public Class frmXMLViewer
    Private Shared strFilePath_ As String
    Private Shared strFindString_ As String
    Private Shared blnFileReadOnly_ As Boolean
    ''' <summary>
    ''' Caminho do arquivo XML a ser aberto
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property FilePath() As String
        Set(ByVal value As String)
            strFilePath_ = value
        End Set
    End Property
    ''' <summary>
    ''' String a ser procurada
    ''' </summary>
    ''' <value></value>
    ''' <remarks></remarks>
    Public WriteOnly Property FindString() As String
        Set(ByVal value As String)
            strFindString_ = value
        End Set
    End Property

    Public WriteOnly Property FileReadOnly() As Boolean
        Set(ByVal value As Boolean)
            blnFileReadOnly_ = value
        End Set
    End Property

    Public Sub Open()
        If Not File.Exists(strFilePath_) Then
            Call MsgBox("Arquivo não encontrado! [" & strFilePath_ & "]", MsgBoxStyle.Exclamation)
            Exit Sub
        End If
        Dim frmChild As New frmXMLViewer()
        With frmChild
            .MdiParent = MDIMain
            .Icon = MDIMain.Icon
            .BringToFront()
            .Name = Name
            .Text = strFilePath_
            .Show()
        End With
    End Sub

    Private Sub frmXMLViewer_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Call ScintillaConfig()
        Call ReadXMLFile()
        Call SctinllaFindString()
    End Sub

    Private Sub SctinllaFindString()
        Dim myList As Object
        If Not IsEmpty(strFindString_) Then
            With txtXML
                myList = .FindReplace.FindAll(strFindString_, ScintillaNet.SearchFlags.WordStart)
                .FindReplace.MarkAll(myList)
                .FindReplace.HighlightAll(myList)
            End With
        End If
    End Sub

    Private Sub ReadXMLFile()
        Try
            Dim w As New MemoryStream()
            Dim myXMLWriter As New XmlTextWriter(w, Nothing)
            Dim myXML As New XmlDocument()
            '--
            myXML.Load(strFilePath_)
            myXMLWriter.Formatting = Formatting.Indented
            myXML.WriteContentTo(myXMLWriter)
            '--
            myXMLWriter.Flush()
            w.Seek(0L, SeekOrigin.Begin)
            '--
            Dim SReader As New StreamReader(w)
            '-- SCINTILLA
            With txtXML
                .Text = SReader.ReadToEnd()
                .Update()
                .IsReadOnly = blnFileReadOnly_
            End With

        Catch ex As Exception
            Call SendError(ex.Message, ex.StackTrace)
        End Try
    End Sub

    Private Sub ScintillaConfig()
        '--
        '-- http://scintillanet.codeplex.com/SourceControl/changeset/view/63427#1051588
        '--
        With txtXML
            .ConfigurationManager.CustomLocation = "ScintillaNET.xml"
            '.ConfigurationManager.Language = "pgsql"
            .ConfigurationManager.Language = "xml"
            '-- line numbers
            .Margins.Margin0.Width = 30
            '-- line markes
            .Margins.Margin1.IsClickable = True
            .Margins.Margin1.Width = 16
            '-- toggle code 
            .Margins.Margin2.Width = 20

            .Styles.BraceLight.ForeColor = Color.Red
            .Styles.BraceLight.BackColor = Color.Black

            '-- apresenta CrLF no fim da linha
            '.EndOfLine.IsVisible = True

            '-- fundo da linha atual 
            .Caret.CurrentLineBackgroundAlpha = 25
            .Caret.CurrentLineBackgroundColor = Color.Yellow
            .Caret.HighlightCurrentLine = True

            '-- identação
            .Indentation.IndentWidth = 4
            .Indentation.ShowGuides = True
            .Indentation.SmartIndentType = ScintillaNet.SmartIndent.CPP
            .Indentation.UseTabs = False

            '--
            .IsBraceMatching = True

            .Update()
        End With
    End Sub

    Private Sub XMLIdentacao()
        Try
            Dim w As New MemoryStream()
            Dim myWriter As New XmlTextWriter(w, Nothing)

            Dim document As New XmlDocument()
            document.LoadXml(txtXML.Text)
            myWriter.Formatting = Formatting.Indented
            document.WriteContentTo(myWriter)

            myWriter.Flush()
            w.Seek(0L, SeekOrigin.Begin)

            Dim reader As New StreamReader(w)

            txtXML.Text = reader.ReadToEnd()
            txtXML.Update()
        Catch ex As Exception
            Call MsgBox(ex.Message, MsgBoxStyle.Exclamation)
        End Try
    End Sub
End Class